﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace working
{
    public partial class QButtonForm : Form
    {
        public Action onClose { get; set; }
        private TextBox[] btnRenameBoxs;
        private RichTextBox[] btnCmdBoxs;
        private const int btnSize = 5;
        private ConfigLogic cfgLogic { get { return ConfigLogic.Instence; } }
        private List<CmdItemLeaf> btnCmds { get { return cfgLogic.btnCmds; } }

        public QButtonForm()
        {
            InitializeComponent();
            btnRenameBoxs = new TextBox[] {
                textBox_qkbtnname1,
                textBox_qkbtnname2,
                textBox_qkbtnname3,
                textBox_qkbtnname4,
                textBox_qkbtnname5,
            };
            btnCmdBoxs = new RichTextBox[] {
                richTextBox_qkbtncmd1,
                richTextBox_qkbtncmd2,
                richTextBox_qkbtncmd3,
                richTextBox_qkbtncmd4,
                richTextBox_qkbtncmd5,
            };
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            SetCmds(cfgLogic.GetQuickBtnCmds());

        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            RecordCmds();
            cfgLogic.SaveQuickBtnCmds();
            if (onClose != null)
                onClose.Invoke();
        }

        private void RecordCmds()
        {
            for (int i = 0; i < btnSize; i++)
            {
                if (btnCmds.Count <= i) btnCmds.Add(new CmdItemLeaf());

                if (btnRenameBoxs.Length > i)
                {
                    if (!string.IsNullOrEmpty(btnRenameBoxs[i].Text))
                    {
                        btnCmds[i].name = btnRenameBoxs[i].Text;
                    }

                }
                if (btnCmdBoxs.Length > i)
                {
                    if (!string.IsNullOrEmpty(btnCmdBoxs[i].Text))
                    {
                        btnCmds[i].text = btnCmdBoxs[i].Text;
                    }
                }
            }
        }

        private void SetCmds(CmdItemLeaf[] cmds)
        {
            for (int i = 0; i < btnSize; i++)
            {
                if (cmds.Length > i && btnRenameBoxs.Length > i)
                {
                    btnRenameBoxs[i].Text = cmds[i].name;
                }
                if (cmds.Length > i && btnCmdBoxs.Length > i)
                {
                    btnCmdBoxs[i].Text = cmds[i].text;
                }
            }
        }
    }
}
